<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>下载界面</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			html,
			body {
				margin: 0;
				padding: 0;
			}
			
			h4,
			h5 {
				margin-left: 15px;
				margin-top: 15px;
			}
			h5 {
				color: #000;
			}
			
			#progress {
				width: 90%;
				left: 15px;
				margin-top: 70px;
			}
			
			#progress span {
				background-color: #ff512f;
			}
		</style>
	</head>

	<body>
		<h4>runface升级</h4>
		<h5>正在下载新版本runface</h5>
		<h5>已完成&nbsp;&nbsp;<span id="per">0%</span></h5>
		<div id="progress" class="mui-progressbar">
			<span></span>
		</div>
		<script src="js/mui.min.js"></script>
		<script type="text/javascript">
			mui.init();
			var dw; //下载对象
			var oper=document.getElementById("per");
			document.addEventListener("plusready", function() {
				var self = plus.webview.currentWebview();
				//得到apk地址
				var updateDownloadUrl = self.upgurl;
				mui.currentWebview.show("pop-in", 100);
				//				mui("#progress").progressbar().setProgress(50);
				var dw = plus.downloader.createDownload(updateDownloadUrl, {
						filename: "_doc/update/",
						timeout: 30
					},
					function(d, status) {
						//console.log(parseInt(d.downloadedSize / d.totalSize * 100));
						if(status == 200) {
//							plus.io.resolveLocalFileSystemURL(d.filename, function(entry) {
//								console.log(entry.toLocalURL());
//							});
							installUpdate(d.filename);
						}
					});
				dw.addEventListener("statechanged", onStateChanged, false);
				dw.start();
			});

            //安装下载的安装包
			function installUpdate(path) {
				plus.nativeUI.showWaiting("安装更新文件...");
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			}

			function onStateChanged(dw,status) {
				if(dw) {
					var per = parseInt(dw.downloadedSize / dw.totalSize * 100);
					if(!isNaN(per)){
						oper.innerText=per+"%";
					}
					
					mui("#progress").progressbar().setProgress(per);
				}
			}
		</script>
	</body>

</html>